﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_TOOL.Tool.Forms;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.Tool.UserControls
{
	public partial class ucMistake : DevExpress.XtraEditors.XtraUserControl
	{
		private int index = 0;
		public int _action = 1;
        MistakeHelper helper=new MistakeHelper(new Mistake());
        DXPopupMenu menu;

		#region Handle Methods

		public ucMistake()
		{
            InitializeComponent();
            InitPopupMenu();

            BindDatatoGrid();
            gridView1.BestFitColumns();
            this.dropbtExport.DropDownControl = menu;
		}

		private void btAddNew_Click_1(object sender, EventArgs e)
		{
			AddNew();
		}

		private void btEdit_Click_1(object sender, EventArgs e)
		{
			EditRow();
		}

		private void btDelete_Click_1(object sender, EventArgs e)
		{
			DeleteRows();
		}


		private void gridView1_CustomDrawEmptyForeground_1(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
		{
			if(gridView1.RowCount != 0)
				return;

			StringFormat drawFormat = new StringFormat();

			drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

			e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

		}

		private void gridView1_DoubleClick_1(object sender, EventArgs e)
		{
			var gridView = (GridView)sender;
			var point = gridView.GridControl.PointToClient(Control.MousePosition);

			DoDoubleClick(gridView, point);
		}


		private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AddNew();
		}

		private void editToolStripMenuItem_Click(object sender, EventArgs e)
		{
			EditRow();
		}

		private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DeleteRows();
		}
		#endregion

		public void BindDatatoGrid()
		{
			MistakeHelper helper = new MistakeHelper(new Mistake());
			DataTable dt = helper.GetAll();

			gridControl1.DataSource = dt;
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
		}

		private void DoDoubleClick(GridView v, Point p)
		{
			_action = 1;
			index = gridView1.FocusedRowHandle;
			var info = v.CalcHitInfo(p);
			if(info.InRow || info.InRowCell)
			{
				var row = new DataRow[gridView1.SelectedRowsCount];
				row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
				string mistakeId = row[0][ToolConstants.Entities.Mistake.FieldName.MistakeID].ToString();
			    helper.ValueObject.MistakeID = mistakeId;
                helper.LoadOne();

				frm_MistakeEdit fr = new frm_MistakeEdit(this);
                fr.MistakeId = helper.ValueObject.MistakeID;
                fr.MistakeName = helper.ValueObject.MistakeEnglish;
                fr.MistakeNameJapan = helper.ValueObject.MistakeJanpanese;

                fr.StartPosition = FormStartPosition.CenterParent;
				fr.ShowDialog();
			}
		}

		private void DeleteRows()
		{
			if(gridView1.SelectedRowsCount >0)
			{
				var row = new DataRow[gridView1.SelectedRowsCount];
				var mistakeId = new string[gridView1.SelectedRowsCount];

				for(int i=0; i< gridView1.SelectedRowsCount; i++)
				{
					row[i]= gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
					mistakeId[i] = row[i][ToolConstants.Entities.Mistake.FieldName.MistakeID].ToString();
				}


				if(MessageBox.Show(ToolConstants.Message.DeleteQuestion, ToolConstants.Form.MessageBox.Warning,
								MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
				{
					MistakeHelper helper = new MistakeHelper(new Mistake());
					for(int i = 0; i < mistakeId.Length; i++)
					{
						helper.ValueObject.MistakeID = mistakeId[i];
						if(helper.Delete() >0)
						{
							_action = 2;
						}
						else
						{
							MessageBox.Show(ToolConstants.Message.DeleteFailed, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}
                    BindDatatoGrid();


				}
			}
			else
			{
				MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
								MessageBoxIcon.Information);
			}
		}

		private void EditRow()
		{
			if(gridView1.SelectedRowsCount >0)
			{
				frm_MistakeEdit fr = new frm_MistakeEdit(this);
				var row = new DataRow[gridView1.SelectedRowsCount];
				row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);

				index = gridView1.FocusedRowHandle;
				_action = 1;

				string mistakeId = row[0][ToolConstants.Entities.Mistake.FieldName.MistakeID].ToString();
                helper.ValueObject.MistakeID = mistakeId;
                helper.LoadOne();

                fr.MistakeId = helper.ValueObject.MistakeID;
                fr.MistakeName = helper.ValueObject.MistakeEnglish;
                fr.MistakeNameJapan = helper.ValueObject.MistakeJanpanese;

                fr.StartPosition = FormStartPosition.CenterParent;
				fr.ShowDialog();
			}
			else
			{
				MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
								MessageBoxIcon.Information);
			}
		}

		private void AddNew()
		{
			Cursor.Current = Cursors.WaitCursor;
			Form fr = new frm_MistakeAdd(this);
            fr.StartPosition = FormStartPosition.CenterParent;
			fr.ShowDialog();
			Cursor.Current = Cursors.Default;
		}

        private void btRefresh_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindDatatoGrid();
            Cursor.Current = Cursors.Default;
        }

        private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DeleteRows();
            }
        }

        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemClickExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemClickExportRows), null, null, null, null));

        }
        void OnItemClickExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemClickExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }
	}
}
